//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include "EtherAddr.hpp"
	/*
	 * Function Name: GetMacAddress
	 * Gets the mac address
	 * Parameters :
	 * [out] : szMacAddress The mac address
	 * Return :
	 * TRUE if Successful
	 * FALSE if failure
	 * 
	*/
TCHAR * _GetMacAddress(BOOL bFormated);

TCHAR *GetFormatedMacAddress()
{
	return _GetMacAddress(TRUE);
}

TCHAR *GetMacAddress()
{
	return _GetMacAddress(FALSE);
}

TCHAR * _GetMacAddress(BOOL bFormated)
{
	    //IP_ADAPTER_INFO AdapterInfo;
		TCHAR *szMacAddress = NULL;
	    TCHAR tempStr[10] = {0};
		PIP_ADAPTER_INFO pAdapterInfo = NULL;//&AdapterInfo;
		ULONG ulSizeAdapterInfo = 0;
		DWORD dwReturnvalueGetAdapterInfo;
		UINT index=0;
		
		SetLastError(0);
		dwReturnvalueGetAdapterInfo = GetAdaptersInfo( pAdapterInfo, &ulSizeAdapterInfo );
		
		

	//Check whether the GetAdaptersInfo succeeded 
	if ( dwReturnvalueGetAdapterInfo == ERROR_BUFFER_OVERFLOW)
	{
		if (!(pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulSizeAdapterInfo)))
		{
			RETAILMSG(1,(L"Insufficient Memory\n "));
			goto ERROR_TAG;
		}

		//Try GetAdaptersInfo again
		SetLastError(0);	
		dwReturnvalueGetAdapterInfo = GetAdaptersInfo( pAdapterInfo, &ulSizeAdapterInfo);
		if (dwReturnvalueGetAdapterInfo != ERROR_SUCCESS)
		{
			RETAILMSG(1,(L"Error in GetAdaptersInfo\n"));
			goto ERROR_TAG;
        }
	}

	//GetAdaptersInfo Failed 
	else if (dwReturnvalueGetAdapterInfo != ERROR_SUCCESS) 
	{
		RETAILMSG(1,(L"Error in GetAdaptersInfo"));
		goto ERROR_TAG;
     
	}

	if ( pAdapterInfo == NULL )
	{
		RETAILMSG(1,(L"PIP_ADAPTER_INFO is NULL after getAdapterInfo. Error Is:%d\n",GetLastError()));
		goto ERROR_TAG;
	}

	szMacAddress = (TCHAR *)malloc(sizeof(TCHAR) * (pAdapterInfo->AddressLength+1+5)*2);
	if (szMacAddress == NULL)
	{
		RETAILMSG(1,(L"The Out Parameter is not allocated"));
		goto ERROR_TAG;
	}

	memset(szMacAddress,'\0',(pAdapterInfo->AddressLength+1+5)*sizeof(TCHAR)*2);


    for (index = 0; index < pAdapterInfo->AddressLength; index++) 
	{

		wsprintf(tempStr,L"%02X",pAdapterInfo->Address[index]);
		wcscat(szMacAddress,tempStr);

		if ( bFormated && index != pAdapterInfo->AddressLength - 1)
		{
			wcscat(szMacAddress, TEXT("-"));
		}
	}
	wcscat(szMacAddress,L"\0");

	if (pAdapterInfo != NULL)
		free(pAdapterInfo);
	return szMacAddress;
ERROR_TAG:
	if(pAdapterInfo !=NULL)
		free(pAdapterInfo);
	return NULL;

	
}

/*int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{
	TCHAR *macAddress;

	RETAILMSG(1,(L"Getting the mac address\n"));
	if((macAddress = GetMacAddress()) ==NULL)
		return FALSE;
	DEBUGMSG(1,(L"Mac Adress in Main %s\n",macAddress));
	free(macAddress);
	return TRUE;
}*/
 
